<!DOCTYPE html>
<html>
<!--
Copyright 2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
  <title>Google Earth API Samples - Batch function calls</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <!-- You should probably remove most of this extra cruft if you're copying
       this sample! -->
  <style type="text/css">@import "static/examples.css";</style>
  <style type="text/css">@import "static/prettify/prettify.css";</style>
  <script type="text/javascript" src="static/prettify/prettify.js"></script>
  
  <!-- Change the key below to your own Maps API key -->
  <script type="text/javascript" src="http://www.google.com/jsapi?hl=en&amp;key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
  <script type="text/javascript">
  /* <![CDATA[ */
  var ge;
  var placemark;
  
  google.load("earth", "1");

  function init() {
    google.earth.createInstance('map3d', initCB, failureCB);
  }
  
  function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
    
    // add a navigation control
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    
    // add some layers
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

    document.getElementById('installed-plugin-version').innerHTML =
      ge.getPluginVersion().toString();
  }
  
  function failureCB(errorCode) {
  }
  
  function run() {
    // Create a swirly line string with 1000 coordinates, all in JavaScript.
    if (placemark)
      ge.getFeatures().removeChild(placemark);
  
    var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    placemark = ge.createPlacemark('');
    var lineString = ge.createLineString('');
    lineString.setTessellate(true);
  
    var coords = lineString.getCoordinates();
    for (var i = 0; i < 1000; i++) {
      coords.pushLatLngAlt(
          lookAt.getLatitude() + Math.cos(i / 10) * i / 10,
          lookAt.getLongitude() + Math.sin(i / 10) * i / 10,
          0);
    }
  
    placemark.setGeometry(lineString);
    ge.getFeatures().appendChild(placemark);
  }
  function runUnbatched() {
    // Run the function unbatched and measure run time.
    var startTime = new Date().getTime();
    run();
    document.getElementById('status-unbatched').innerHTML =
        (new Date().getTime() - startTime) + 'ms';
  }
  
  function runBatched() {
    // Run the function batched and measure run time.
  
    // NOTE: google.earth.executeBatch is guaranteed
    // to be synchronous.
    var startTime = new Date().getTime();
    google.earth.executeBatch(ge, function() {
      run();
      document.getElementById('status-batched').innerHTML =
          (new Date().getTime() - startTime) + 'ms';
    });
  }
  
  /* ]]> */
  </script>
</head>
<body onload="if(window.prettyPrint)prettyPrint();init();">
  <h1>Google Earth API Samples - Batch function calls</h1>
  <dl>
            <dt>Last Modified:</dt><dd>08/25/2009</dd>
    <dt>Installed Plugin Version:</dt><dd id="installed-plugin-version">...</dd>
  </dl>
  <div style="clear: both;"></div>
  
  <div id="ui" style="position: relative;">
    <div id="map3d" style="border: 1px solid silver; width: 500px; height: 500px;"></div>

    <div id="extra-ui" style="position: absolute; left: 520px; top: 0;">
      <table border="1" cellspacing="0" cellpadding="0">
        <tr><th style="text-align: center"><input type="button" onclick="runUnbatched()" value="Unbatched calls"/></th>
            <th style="text-align: center"><input type="button" onclick="runBatched()" value="Batched calls"/></th></tr>
        <tr><td style="text-align: center"><span id="status-unbatched">--</span></td>
            <td style="text-align: center"><span id="status-batched">--</span></td></tr>
      </table>
      
      <h2>Relevant Resources:</h2>
      <ul>
<li><a href="http://code.google.com/apis/earth/documentation/reference/google_earth_namespace.html">google.earth Namespace Reference</a></li>
      </ul>
      <h2>Relevant Code Excerpt:</h2>      <pre class="prettyprint lang-js">function runUnbatched() {
  // Run the function unbatched and measure run time.
  var startTime = new Date().getTime();
  run();
  document.getElementById('status-unbatched').innerHTML =
      (new Date().getTime() - startTime) + 'ms';
}

function runBatched() {
  // Run the function batched and measure run time.

  // NOTE: google.earth.executeBatch is guaranteed
  // to be synchronous.
  var startTime = new Date().getTime();
  google.earth.executeBatch(ge, function() {
    run();
    document.getElementById('status-batched').innerHTML =
        (new Date().getTime() - startTime) + 'ms';
  });
}</pre>      <pre class="prettyprint lang-js">var placemark;</pre>      <pre class="prettyprint lang-js">function run() {
  // Create a swirly line string with 1000 coordinates, all in JavaScript.
  if (placemark)
    ge.getFeatures().removeChild(placemark);

  var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
  placemark = ge.createPlacemark('');
  var lineString = ge.createLineString('');
  lineString.setTessellate(true);

  var coords = lineString.getCoordinates();
  for (var i = 0; i &lt; 1000; i++) {
    coords.pushLatLngAlt(
        lookAt.getLatitude() + Math.cos(i / 10) * i / 10,
        lookAt.getLongitude() + Math.sin(i / 10) * i / 10,
        0);
  }

  placemark.setGeometry(lineString);
  ge.getFeatures().appendChild(placemark);
}</pre>    </div>
  </div>
</body>
</html>
